﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Configuration;

namespace Wicresoft.RDW.UserControls
{
    public partial class PagerControl : System.Web.UI.UserControl
    {
        public delegate void PageRefresh(int pageIndex);
        public event PageRefresh PageChange;

        private int TotalCount
        {
            set
            {
                ViewState["TotalCount"] = value;
            }
            get
            {
                if (ViewState["TotalCount"] == null)
                {
                    return 0;
                }
                return (int)ViewState["TotalCount"];
            }
        }
        public int CurrentPageIndex
        {
            set
            {
                ViewState["CurrentPageIndex"] = value;
            }
            get
            {
                if (ViewState["CurrentPageIndex"] == null)
                {
                    return 0;
                }
                return (int)ViewState["CurrentPageIndex"];
            }
        }
        
        public int PageSize
        {
            get
            {
                if (ViewState["PageSize"] == null)
                {
                    return 10;
                }
                else
                {
                    return (int)ViewState["PageSize"];
                }

            }
            set
            {
                ViewState["PageSize"] = value;
            }

        }

        public int TotalPage
        {
            get
            {
                if (TotalCount % PageSize != 0)
                {
                    return TotalCount / PageSize + 1;
                }
                else
                {
                    return TotalCount / PageSize;
                }
            }
        }

        private void IntialCulture()
        {
            this.FirstPage.Text = Resources.NameResource.FirstPage;
            this.NextPage.Text = Resources.NameResource.NextPage;
            this.PreviousPage.Text = Resources.NameResource.PreviousPage;
            this.LastPage.Text = Resources.NameResource.LastPage;
            if (TotalCount == 0)
            {
                this.lblCurrentPage.Text = string.Format(Resources.NameResource.CurrentPageIndex, CurrentPageIndex);
            }
            else
            {
                this.lblCurrentPage.Text = string.Format(Resources.NameResource.CurrentPageIndex, (CurrentPageIndex + 1));
            }
            
            this.lblToatlPage.Text = string.Format(Resources.NameResource.TotalPage, TotalPage);
        }

        protected void FirstPage_Click(object sender, EventArgs e)
        {
            this.CurrentPageIndex = 0;
            this.lblCurrentPage.Text = string.Format(Resources.NameResource.CurrentPageIndex, (CurrentPageIndex + 1));
            PageChange(CurrentPageIndex);
        }
        protected void PreviousPage_Click(object sender, EventArgs e)
        {

            this.CurrentPageIndex -= 1;
            this.lblCurrentPage.Text = string.Format(Resources.NameResource.CurrentPageIndex, (CurrentPageIndex + 1));
            PageChange(CurrentPageIndex);
        }
        protected void NextPage_Click(object sender, EventArgs e)
        {

            this.CurrentPageIndex += 1;
            this.lblCurrentPage.Text = string.Format(Resources.NameResource.CurrentPageIndex, (CurrentPageIndex + 1));
            PageChange(CurrentPageIndex);
        }
        protected void LastPage_Click(object sender, EventArgs e)
        {
            this.CurrentPageIndex = (TotalPage - 1);
            this.lblCurrentPage.Text = string.Format(Resources.NameResource.CurrentPageIndex, (CurrentPageIndex + 1));
            PageChange(CurrentPageIndex);
        }
        private void IntialButton()
        {
            if (TotalPage <= 1)
            {
                this.FirstPage.Enabled = false;
                this.PreviousPage.Enabled = false;
                this.NextPage.Enabled = false;
                this.LastPage.Enabled = false;
            }
            else
            {
                if (CurrentPageIndex == (TotalPage - 1))
                {
                    this.FirstPage.Enabled = true;
                    this.PreviousPage.Enabled = true;
                    this.NextPage.Enabled = false;
                    this.LastPage.Enabled = false;
                }
                else if (CurrentPageIndex == 0)
                {
                    this.NextPage.Enabled = true;
                    this.LastPage.Enabled = true;
                    this.FirstPage.Enabled = false;
                    this.PreviousPage.Enabled = false;
                }
                else
                {
                    this.NextPage.Enabled = true;
                    this.LastPage.Enabled = true;
                    this.FirstPage.Enabled = true;
                    this.PreviousPage.Enabled = true;
                }
            }

        }
        public void IntialProperties(int totalCount)
        {
            this.TotalCount = totalCount;
            this.lblToatlPage.Text = string.Format(Resources.NameResource.TotalPage, TotalPage);
            IntialButton();
            IntialCulture();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            var pageIndex = this.txtSelectPageIndex.Text;
            if (string.IsNullOrEmpty(pageIndex) || !Regex.IsMatch(pageIndex, @"\d"))
            {
                return;
            }
            int index = int.Parse(pageIndex);
            if (index < 1 || index > TotalPage)
            {
                return;
            }
            this.CurrentPageIndex = index - 1;
            this.lblCurrentPage.Text = string.Format(Resources.NameResource.CurrentPageIndex, (CurrentPageIndex + 1));
            PageChange(CurrentPageIndex);
        }

    }
}